package com.ztjava.common.controller;

import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.Ret;
import com.ztjava.common.model.push.PushRecord;
import com.ztjava.common.model.push.PushTemplate;
import com.ztjava.common.service.PushService;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.bean.sub.PushBean;
import com.ztjava.core.client.PortalClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;
import com.ztjava.plugin.event.EventKit;
import com.ztjava.plugin.event.EventTag;
import com.ztjava.plugin.event.core.RequestEvent;

@ControllerClass(path="/common/push",client=PortalClient.class)
@UserController(portal=Constant.P_PORTAL)
public class PushPortalController extends BaseController{
	/*************
	 *PushAdminController.java
	 *@ztjava.com
	 *2017-11-09 14:25:09
	**************/
	private static PushService pushService = Duang.duang(PushService.class);
	/*************
	 *修改Template
	**************/
	@UserMethod("CommonPushTemplate")
	public void addEditTemplate(){
		Integer id = getParaToInt("id",0);
		PushTemplate pushTemplate = new PushTemplate();
		if(id>0){
			pushTemplate = PushTemplate.dao.findById(id);
		}
		setAttr("pushTemplate",pushTemplate);
		render("addEditTemplate.html");
	}

	/*************
	 *保存Template
	**************/
	@UserMethod("CommonPushTemplate")
	public void saveTemplate(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		PushTemplate pushTemplate = getModel(PushTemplate.class);
		Ret ret = pushService.checkRepeat(pushTemplate,PushTemplate.tablename
			,""		,new String[]{"code","编码不能重复！"});
		if(ret.get(Constant.RESULT).equals(Constant.ERROR)){
			renderError(ret.get(Constant.MESSAGE).toString());
			return;
		}
		saveModel(pushTemplate,authSession.getId());
		
		pushService.loadTemplateCache();
		renderSuccess();
	}

	/*************
	 *Template列表
	**************/
	@UserMethod("CommonPushTemplate")
	public void templateList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("templateList.html");
			return;
		}

		
		renderPageJson(pushService.getTemplatePage(client, getParaMap()));
	}

	/*************
	 *批量删除Template
	**************/
	@UserMethod("CommonPushTemplate")
	public void batchDeleteTemplate(){
		String[] btSelectItem = getParaValues("btSelectItem");
		pushService.batchDelete(PushTemplate.tablename, btSelectItem);
		
		pushService.loadTemplateCache();
		renderSuccess();
	}
	/*************
	 *Record列表
	**************/
	@UserMethod("CommonPushTemplate")
	public void recordList(){
		Integer template_id = getParaToInt("template_id");
		setAttr("template_id",template_id);
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("recordList.html");
			return;
		}

		
		renderPageJson(pushService.getRecordPage(client, getParaMap()));
	}

	/*************
	 *批量删除Record
	**************/
	@UserMethod("CommonPushTemplate")
	public void batchDeleteRecord(){
		String[] btSelectItem = getParaValues("btSelectItem");
		pushService.batchDelete(PushRecord.tablename, btSelectItem);
		renderSuccess();
	}

	@UserMethod("CommonPushTemplate")
	public void testTemplate(){
		Integer id = getParaToInt("id");
		PushTemplate pushTemplate = PushTemplate.dao.findById(id);
		setAttr("pushTemplate", pushTemplate);
		render("testTemplate.html");
	}

	@UserMethod("CommonPushTemplate")
	public void doTestTemplate(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		Integer id = getParaToInt("id");
		String to = getPara("to","");
		if(to.equals("")){
			renderError("必须填写接收人!");
			return;
		}
		PushTemplate pushTemplate = PushTemplate.dao.findById(id);
		PushBean pushBean = new PushBean(pushTemplate.getStr("code"),
				"Common", authSession.getName(), to, authSession.getId(), new String[][]{});
		List<Ret> retList = EventKit.postRemote(EventTag.COMMON_PUSH_SYNC, new RequestEvent(pushBean));
		if(retList.size()>0){
			renderRet(retList.get(0));
			return;
		}
		renderError("没有响应！");
	}
	
}
